package com.chengwang.bookstory.service.Imple;

import com.chengwang.bookstory.mapper.BookMapper;
import com.chengwang.bookstory.pojo.Book;
import com.chengwang.bookstory.pojo.BookUpload;
import com.chengwang.bookstory.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class BookServiceImple implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public List<Book> queryByBookType(String bookType) {
        return bookMapper.queryByBookType(bookType);
    }

    @Override
    public List<Book> queryByBookName(String searchMessage) {
        return bookMapper.queryByBookName(searchMessage);
    }

    @Override
    public List<Book> queryByAnyType(String searchMessage) {
        return bookMapper.queryByAnyType(searchMessage);
    }

    @Override
    public List<Book> queryAllBook() {
        return bookMapper.queryAllBook();
    }

    @Override
    public Book queryBookById(String bookId) {
        return bookMapper.queryBookById(bookId);
    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }

    @Override
    public void upload(BookUpload bookUpload, HttpServletRequest request) {
        Book book = new Book();
        String bookId= String.valueOf(System.currentTimeMillis());
        book.setBookId(bookId);
        book.setBookName(bookUpload.getBookName());
        book.setBookWriter(bookUpload.getBookWriter());
        book.setBookNumber(bookUpload.getBookNumber());
        book.setBookPrice(bookUpload.getBookPrice());
        book.setBookType(bookUpload.getBookType());
        book.setBookDetail(bookUpload.getBookDetail());
        MultipartFile bfile =bookUpload.getFile();
        System.out.println(bfile.isEmpty());
        if (bfile.isEmpty() || bfile==null){
           book.setBookAddress("../image/book0.png");
       }else {
            String originalFilename = bfile.getOriginalFilename();
            String tamp= String.valueOf(System.currentTimeMillis());
            String newName=tamp+"_"+originalFilename;
            String path=this.getClass().getClassLoader().getResource("static/image").getPath()+"/upload";
            File file1 = new File(path, newName);
            if(!file1.getParentFile().exists()){
                file1.getParentFile().mkdirs();
            }
            try {
                bfile.transferTo(file1);
            } catch (IOException e) {
                System.out.println("文件加载出现问题 "+e);
            }
            book.setBookAddress("../image/upload/"+newName);
        }

       bookMapper.upload(book);
    }

    @Override
    public List<Book> queryBookByBookName(String bookName) {
        return bookMapper.queryBookByBookName(bookName);
    }
}
